{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:39.094561Z",
     "start_time": "2020-08-20T15:55:38.407272Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "from requests_html import HTMLSession\n",
    "import pandas as pd\n",
    "import traceback\n",
    "\n",
    "from lxml import etree\n",
    "import requests\n",
    "\n",
    "import re\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "#matplotlib设置输出矢量图\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'svg'\n",
    "\n",
    "# 可交互的matplotlib\n",
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.485581Z",
     "start_time": "2020-08-20T15:55:39.096083Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Code review', 'Project management', 'Integrations', 'Actions', 'Packages', 'Security', 'Team management', 'Hosting', 'Mobile', 'Customer stories ', 'Security ', 'Team', 'Enterprise', 'Explore GitHub ', 'Topics', 'Collections', 'Trending', 'Learning Lab', 'Open source guides', 'Events', 'Community forum', 'GitHub Education', 'Marketplace', 'Compare plans', 'Contact Sales', 'Nonprofit ', 'Education ', '\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n  ', '\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n  ', '\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n\\n    ', '\\n  ', '\\n    ', '\\n    Watch\\n', '\\n      2\\n    ', '\\n      ', '\\n      Star\\n', '\\n      59\\n    ', '\\n        ', '\\n        Fork\\n', '\\n      15\\n    ', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n              ', '\\n            ', '\\n              ', '\\n', '\\n                    Code\\n', '\\n                    Pull requests\\n', '\\n                    Actions\\n', '\\n                    Projects\\n', '\\n                    Security\\n', '\\n                    Insights\\n', '\\n                ', '\\n                Open with GitHub Desktop\\n', '\\n                ', '\\n                Download ZIP\\n', '\\n              ', '\\n              ', '\\n            ', 'C (ANSI) (32 keywords)', 'C (C18) (44 keywords)', 'C# (ISO/IEC 23270:2018) (77 keywords)', 'C++ (C++17) (73 keywords)', 'Dart (1) (33 keywords)', 'Elixir (1.10) (15 keywords)', 'Erlang (23) (27 keywords)', 'Fortran (Fortran 2008) (103 keywords)', 'Go (1.14) (25 keywords)', 'JS (ES2019) (34 keywords)', 'Java (SE 14) (51 keywords)', 'Kotlin (1.3) (76 keywords)', 'Lua (5.3) (22 keywords)', 'PHP (7.0) (69 keywords)', 'Python (2.7) (31 keywords)', 'Python (3.8) (35 keywords)', 'R (3.5) (21 keywords)', 'Ruby (2.7) (41 keywords)', 'Rust (1.43) (40 keywords)', 'Scala (2.13) (40 keywords)', 'Swift (5.2) (96 keywords)', '\\n        ', '\\n        ', '\\n        ', '\\n      ', 'Terms', 'Privacy', 'Security', 'Status', 'Help', 'Contact GitHub', 'Pricing', 'API', 'Training', 'Blog', 'About']\n"
     ]
    }
   ],
   "source": [
    "headers1 = {\n",
    "    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
    "    'accept-encoding': 'gzip, deflate, br',\n",
    "    'accept-language': 'zh-CN,zh;q=0.9',\n",
    "    'cache-control': 'no-cache',\n",
    "    # 'cookie':  'UM_distinctid=172b674e5f71cb-034c5dfe424061-3a365305-1fa400-172b674e5f8848; PHPSESSID=4gsduu0v6tmi1lkbsf8cp7kej0; local_zone=320100%7Cnanjing%7C%C4%CF%BE%A9%7C58238; Hm_lvt_d64174522c86449826babe56fb2a88ff=1592199015; CNZZDATA1089612=cnzz_eid%3D725595362-1592200519-https%253A%252F%252Fguilin.cncn.com%252F%26ntime%3D1592200519; Hm_lpvt_d64174522c86449826babe56fb2a88ff=1592203476',\n",
    "    'pragma': 'no-cache',\n",
    "    # 'referer':  'https://beihai.cncn.com/jingdian/beihai/',\n",
    "    'sec-fetch-dest': 'document',\n",
    "    'sec-fetch-mode': 'navigate',\n",
    "    'sec-fetch-site': 'same-site',\n",
    "    'sec-fetch-user': '?1',\n",
    "    'upgrade-insecure-requests': '1',\n",
    "    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',\n",
    "\n",
    "}\n",
    "\n",
    "headers = {\n",
    "    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
    "    'accept-encoding': 'gzip, deflate, br',\n",
    "    'accept-language': 'zh-CN,zh;q=0.9',\n",
    "    'cache-control': 'no-cache',\n",
    "    # 'cookie':  'UM_distinctid=172b674e5f71cb-034c5dfe424061-3a365305-1fa400-172b674e5f8848; PHPSESSID=4gsduu0v6tmi1lkbsf8cp7kej0; local_zone=320100%7Cnanjing%7C%C4%CF%BE%A9%7C58238; Hm_lvt_d64174522c86449826babe56fb2a88ff=1592199015; CNZZDATA1089612=cnzz_eid%3D725595362-1592200519-https%253A%252F%252Fguilin.cncn.com%252F%26ntime%3D1592200519; Hm_lpvt_d64174522c86449826babe56fb2a88ff=1592203476',\n",
    "    'pragma': 'no-cache',\n",
    "    # 'referer':  'https://beihai.cncn.com/jingdian/beihai/',\n",
    "    'sec-fetch-dest': 'document',\n",
    "    'sec-fetch-mode': 'navigate',\n",
    "    'sec-fetch-site': 'same-site',\n",
    "    'sec-fetch-user': '?1',\n",
    "    'upgrade-insecure-requests': '1',\n",
    "    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "def run():\n",
    "    q_url = \"https://github.com/e3b0c442/keywords\"\n",
    "\n",
    "    url_first = q_url\n",
    "    html = requests.get(url_first, headers1)\n",
    "    #print(html.text)\n",
    "\n",
    "    dom = etree.HTML(html.text)\n",
    "    result = dom.xpath('//ul/li/a/text()')\n",
    "    title = result\n",
    "    print(title)\n",
    "    return title\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    data = run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.497345Z",
     "start_time": "2020-08-20T15:55:54.487241Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C (ANSI) (32 keywords)\n",
      "C (C18) (44 keywords)\n",
      "C# (ISO/IEC 23270:2018) (77 keywords)\n",
      "C++ (C++17) (73 keywords)\n",
      "Dart (1) (33 keywords)\n",
      "Elixir (1.10) (15 keywords)\n",
      "Erlang (23) (27 keywords)\n",
      "Fortran (Fortran 2008) (103 keywords)\n",
      "Go (1.14) (25 keywords)\n",
      "JS (ES2019) (34 keywords)\n",
      "Java (SE 14) (51 keywords)\n",
      "Kotlin (1.3) (76 keywords)\n",
      "Lua (5.3) (22 keywords)\n",
      "PHP (7.0) (69 keywords)\n",
      "Python (2.7) (31 keywords)\n",
      "Python (3.8) (35 keywords)\n",
      "R (3.5) (21 keywords)\n",
      "Ruby (2.7) (41 keywords)\n",
      "Rust (1.43) (40 keywords)\n",
      "Scala (2.13) (40 keywords)\n",
      "Swift (5.2) (96 keywords)\n"
     ]
    }
   ],
   "source": [
    "sub_str = \")\"\n",
    "data_result = []\n",
    "\n",
    "for i in data:\n",
    "    i = str(i)\n",
    "    if (i.find(sub_str) > 0):\n",
    "        print(i)\n",
    "        data_result.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.512717Z",
     "start_time": "2020-08-20T15:55:54.498817Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C (ANSI)\n",
      "32\n",
      "C (C18)\n",
      "44\n",
      "C# (ISO/IEC 23270:2018)\n",
      "77\n",
      "C++ (C++17)\n",
      "73\n",
      "Dart (1)\n",
      "33\n",
      "Elixir (1.10)\n",
      "15\n",
      "Erlang (23)\n",
      "27\n",
      "Fortran (Fortran 2008)\n",
      "10\n",
      "Go (1.14)\n",
      "25\n",
      "JS (ES2019)\n",
      "34\n",
      "Java (SE 14)\n",
      "51\n",
      "Kotlin (1.3)\n",
      "76\n",
      "Lua (5.3)\n",
      "22\n",
      "PHP (7.0)\n",
      "69\n",
      "Python (2.7)\n",
      "31\n",
      "Python (3.8)\n",
      "35\n",
      "R (3.5)\n",
      "21\n",
      "Ruby (2.7)\n",
      "41\n",
      "Rust (1.43)\n",
      "40\n",
      "Scala (2.13)\n",
      "40\n",
      "Swift (5.2)\n",
      "96\n"
     ]
    }
   ],
   "source": [
    "names = []\n",
    "values = []\n",
    "dirct = {}\n",
    "\n",
    "for i in data_result:\n",
    "    name = i.find(\") (\")\n",
    "    print(i[:name+1])\n",
    "    print(i[name+3:name+5])\n",
    "    dirct[i[:name+1]] = i[name+3:name+5]\n",
    "    names.append(i[:name+1])\n",
    "    values.append(i[name+3:name+5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.524150Z",
     "start_time": "2020-08-20T15:55:54.514027Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C (ANSI)', 'C (C18)', 'C# (ISO/IEC 23270:2018)', 'C++ (C++17)', 'Dart (1)', 'Elixir (1.10)', 'Erlang (23)', 'Fortran (Fortran 2008)', 'Go (1.14)', 'JS (ES2019)', 'Java (SE 14)', 'Kotlin (1.3)', 'Lua (5.3)', 'PHP (7.0)', 'Python (2.7)', 'Python (3.8)', 'R (3.5)', 'Ruby (2.7)', 'Rust (1.43)', 'Scala (2.13)', 'Swift (5.2)']\n",
      "['32', '44', '77', '73', '33', '15', '27', '10', '25', '34', '51', '76', '22', '69', '31', '35', '21', '41', '40', '40', '96']\n",
      "{'C (ANSI)': '32', 'C (C18)': '44', 'C# (ISO/IEC 23270:2018)': '77', 'C++ (C++17)': '73', 'Dart (1)': '33', 'Elixir (1.10)': '15', 'Erlang (23)': '27', 'Fortran (Fortran 2008)': '10', 'Go (1.14)': '25', 'JS (ES2019)': '34', 'Java (SE 14)': '51', 'Kotlin (1.3)': '76', 'Lua (5.3)': '22', 'PHP (7.0)': '69', 'Python (2.7)': '31', 'Python (3.8)': '35', 'R (3.5)': '21', 'Ruby (2.7)': '41', 'Rust (1.43)': '40', 'Scala (2.13)': '40', 'Swift (5.2)': '96'}\n"
     ]
    }
   ],
   "source": [
    "print(names)\n",
    "print(values)\n",
    "print(dirct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.539390Z",
     "start_time": "2020-08-20T15:55:54.525372Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按值(value)排序:\n",
      "[('Fortran (Fortran 2008)', '10'), ('Elixir (1.10)', '15'), ('R (3.5)', '21'), ('Lua (5.3)', '22'), ('Go (1.14)', '25'), ('Erlang (23)', '27'), ('Python (2.7)', '31'), ('C (ANSI)', '32'), ('Dart (1)', '33'), ('JS (ES2019)', '34'), ('Python (3.8)', '35'), ('Rust (1.43)', '40'), ('Scala (2.13)', '40'), ('Ruby (2.7)', '41'), ('C (C18)', '44'), ('Java (SE 14)', '51'), ('PHP (7.0)', '69'), ('C++ (C++17)', '73'), ('Kotlin (1.3)', '76'), ('C# (ISO/IEC 23270:2018)', '77'), ('Swift (5.2)', '96')]\n"
     ]
    }
   ],
   "source": [
    "print (\"按值(value)排序:\")\n",
    "\n",
    "# sorted(key_value) 返回重新排序的列表\n",
    "# 字典按键排序\n",
    "\n",
    "sort_data = sorted(dirct.items(), key = lambda kv:(kv[1], kv[0]))\n",
    "print(sort_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.551425Z",
     "start_time": "2020-08-20T15:55:54.540802Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Fortran (Fortran 2008)', 'Elixir (1.10)', 'R (3.5)', 'Lua (5.3)', 'Go (1.14)', 'Erlang (23)', 'Python (2.7)', 'C (ANSI)', 'Dart (1)', 'JS (ES2019)', 'Python (3.8)', 'Rust (1.43)', 'Scala (2.13)', 'Ruby (2.7)', 'C (C18)', 'Java (SE 14)', 'PHP (7.0)', 'C++ (C++17)', 'Kotlin (1.3)', 'C# (ISO/IEC 23270:2018)', 'Swift (5.2)']\n",
      "['10', '15', '21', '22', '25', '27', '31', '32', '33', '34', '35', '40', '40', '41', '44', '51', '69', '73', '76', '77', '96']\n"
     ]
    }
   ],
   "source": [
    "x = []\n",
    "y = []\n",
    "\n",
    "for i in sort_data:\n",
    "    x.append(i[0])\n",
    "    y.append(i[1])\n",
    "    \n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T15:55:54.684126Z",
     "start_time": "2020-08-20T15:55:54.553663Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "603f47804cc440078f6823d6075907ac",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.bar(x, y)\n",
    "plt.title('Bar')\n",
    "plt.ylabel('Y axis')\n",
    "plt.xlabel('X axis')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "144px",
    "left": "897.333px",
    "right": "20px",
    "top": "121px",
    "width": "350px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
